if exists (select 1
          from sysobjects
          where  id = object_id('mb_CreateObject')
          and type in ('P','PC'))
   drop procedure mb_CreateObject
go

if exists (select 1
          from sysobjects
          where  id = object_id('mb_DeleteObject')
          and type in ('P','PC'))
   drop procedure mb_DeleteObject
go

if exists (select 1
          from sysobjects
          where  id = object_id('mb_UpdateObject')
          and type in ('P','PC'))
   drop procedure mb_UpdateObject
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('mb_MetabaseStructure') and o.name = 'FK_MB_METAB_REF_MB_METAB_1')
alter table mb_MetabaseStructure
   drop constraint FK_MB_METAB_REF_MB_METAB_1
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('mb_MetabaseStructure') and o.name = 'FK_MB_METAB_REFERENCE_MB_METAB')
alter table mb_MetabaseStructure
   drop constraint FK_MB_METAB_REFERENCE_MB_METAB
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('mb_MetabaseObjectTypes')
            and   name  = 'i_TypeName'
            and   indid > 0
            and   indid < 255)
   drop index mb_MetabaseObjectTypes.i_TypeName
go

if exists (select 1
            from  sysobjects
           where  id = object_id('mb_MetabaseObjectTypes')
            and   type = 'U')
   drop table mb_MetabaseObjectTypes
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('mb_MetabaseStructure')
            and   name  = 'i_parent_id'
            and   indid > 0
            and   indid < 255)
   drop index mb_MetabaseStructure.i_parent_id
go

if exists (select 1
            from  sysobjects
           where  id = object_id('mb_MetabaseStructure')
            and   type = 'U')
   drop table mb_MetabaseStructure
go
